﻿using LyData.Data;
using System;
using System.Data;
using System.Text;
using System.Web;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;


public partial class Admin_ManagerRoleEdit : System.Web.UI.Page
{
    protected void Bind()
    {
        string str = base.Request.QueryString["RoleId"];
        if (str == null)
        {
            this.RoleId.Text = "→ <a href=\"ManagerRole.aspx\">请选择需定义的管理员组</a>";
            this.Msg.Text = "→ <a href=\"ManagerRole.aspx\">请选择需定义的管理员组</a>";
            this.Msg.CssClass = "errorMsg";
            this.Msg.Visible = true;
            this.Edit.Enabled = false;
        }
        else
        {
            IDb db = new DbFactory().DriverDb();
            db.Open();
            DataRow row = db.TopRow("*", "managerRole", string.Format("roleId={0}", str), "");
            DataTable table = db.DataTable("MenuId,MenuName,Attributer", "menu", string.Format("Attributer<901 AND SiteLanguage={0} ", SiteInfo.SiteLanguage()), "Location ASC, OrderId ASC,MenuId ASC");
            for (int i = 0; i < table.Rows.Count; i++)
            {
                DataRow row2 = table.Rows[i];
                string attribute = ((int)row2["Attributer"]).ToString();
                string str3 = ((int)row2["MenuId"]).ToString();
                string str5 = attribute;
                if (str5 == null)
                {
                    goto Label_02D1;
                }
                if (!(str5 == "0"))
                {
                    if (str5 == "1")
                    {
                        goto Label_01CB;
                    }
                    if (str5 == "2")
                    {
                        goto Label_020E;
                    }
                    if (str5 == "3")
                    {
                        goto Label_0251;
                    }
                    if (str5 == "4")
                    {
                        goto Label_0291;
                    }
                    goto Label_02D1;
                }
                DataTable inputTable = db.DataTable("Title,FileName,ParentId,SinglePageId,MenuId", "SinglePage", string.Format("MenuId={0}", str3), "OrderId ASC");
                inputTable.Columns["Title"].ColumnName = "ClassName";
                inputTable.Columns["SinglePageId"].ColumnName = "id";
                goto Label_02D4;
            Label_01CB:
                inputTable = db.DataTable("ClassName,FileName,ParentId,ArticleClassId,MenuId", "articleClass", string.Format("MenuId={0}", str3), "OrderId ASC");
                inputTable.Columns["ArticleClassId"].ColumnName = "id";
                goto Label_02D4;
            Label_020E:
                inputTable = db.DataTable("ClassName,FileName,ParentId,PicClassId,MenuId", "picClass", string.Format("MenuId={0}", str3), "OrderId ASC");
                inputTable.Columns["PicClassId"].ColumnName = "id";
                goto Label_02D4;
            Label_0251:
                inputTable = db.DataTable("ClassName,FileName,ParentId,ProductClassId,MenuId", "productClass", string.Format("MenuId={0}", str3), "OrderId ASC");
                inputTable.Columns["ProductClassId"].ColumnName = "id";
                goto Label_02D4;
            Label_0291:
                inputTable = db.DataTable("MenuName,FileName,MenuId as id,0 as ParentId,MenuId", "MessageConfig", string.Format("MenuId={0}", str3), "");
                inputTable.Columns["MenuName"].ColumnName = "ClassName";
                goto Label_02D4;
            Label_02D1:
                inputTable = null;
            Label_02D4:
                if (inputTable.Rows.Count > 0)
                {
                    TreeNode inputMenuItem = new TreeNode();
                    Function.PopulateManagerTreeNode(inputTable, inputMenuItem, "ClassName", "id", "ParentId", 0, "FileName", attribute, (string)row["RoleList"]);
                    this.ManagerTreeView.Nodes.Add(inputMenuItem.ChildNodes[0]);
                }
            }
            db.Close();
            this.RoleId.Text = (string)row["roleId"];
            this.RoleTitle.Text = (string)row["Title"];
            this.AddTime.Text = (string)row["AddTime"];
            if (row["IsAdmin"].ToString() == "1")
            {
                this.IsAdmin.Checked = true;
            }
            this.OperNode(null, this.HouTaiTreeView, 0, (string)row["RoleList"]);
        }
    }

    protected void Edit_Click(object sender, EventArgs e)
    {
        if (base.IsValid)
        {
            StringBuilder builder = new StringBuilder();
            if (base.Request.Form["checkbox1"] != null)
            {
                builder.AppendFormat("{0},", base.Request.Form["checkbox1"].ToString());
            }
            if (base.Request.Form["checkbox2"] != null)
            {
                builder.AppendFormat("{0},", base.Request.Form["checkbox2"].ToString());
            }
            if (base.Request.Form["checkbox3"] != null)
            {
                builder.AppendFormat("{0},", base.Request.Form["checkbox3"].ToString());
            }
            if (base.Request.Form["checkbox4"] != null)
            {
                builder.AppendFormat("{0},", base.Request.Form["checkbox4"].ToString());
            }
            if (base.Request.Form["checkbox5"] != null)
            {
                builder.AppendFormat("{0},", base.Request.Form["checkbox5"].ToString());
            }
            if (base.Request.Form["checkbox6"] != null)
            {
                builder.AppendFormat("{0},", base.Request.Form["checkbox6"].ToString());
            }
            if (base.Request.Form["checkbox7"] != null)
            {
                builder.AppendFormat("{0},", base.Request.Form["checkbox7"].ToString());
            }
            if (base.Request.Form["checkbox8"] != null)
            {
                builder.AppendFormat("{0},", base.Request.Form["checkbox8"].ToString());
            }
            if (this.ManagerTreeView.CheckedNodes.Count > 0)
            {
                foreach (TreeNode node in this.ManagerTreeView.CheckedNodes)
                {
                    string str = node.Value;
                    if (!builder.ToString().Contains(str + "_"))
                    {
                        builder.AppendFormat("{0},", str);
                    }
                }
            }
            if (this.HouTaiTreeView.CheckedNodes.Count > 0)
            {
                foreach (TreeNode node2 in this.HouTaiTreeView.CheckedNodes)
                {
                    string str2 = node2.Value;
                    builder.AppendFormat("{0},", str2);
                }
            }
            IDb db = new DbFactory().DriverDb();
            db.Open();
            string str3 = "0";
            if (this.IsAdmin.Checked)
            {
                str3 = "1";
            }
            string[,] includeColumns = new string[,] { { "Title", this.RoleTitle.Text.ToString(), "NVarChar", "32" }, { "RoleList", builder.ToString(), "NText", "0" }, { "IsAdmin", str3, "int", "0" } };
            db.Update("managerRole", includeColumns, string.Format("roleId ={0}", this.RoleId.Text));
            db.Close();
            this.ManagerTreeView.Nodes.Clear();
            this.Bind();
            this.Msg.Text = "管理员组权限定义成功！";
            this.Msg.Visible = true;
        }
    }

    public void OperNode(TreeNode myTreeNode, TreeView myTreeView, int i, string listStr)
    {
        if (i == 0)
        {
            foreach (TreeNode node in myTreeView.Nodes)
            {
                if (listStr.Contains(node.Value))
                {
                    node.Checked = true;
                }
                if (node.ChildNodes.Count > 0)
                {
                    this.OperNode(node, myTreeView, 1, listStr);
                }
            }
        }
        else
        {
            foreach (TreeNode node2 in myTreeNode.ChildNodes)
            {
                if (listStr.Contains(node2.Value + ","))
                {
                    node2.Checked = true;
                }
                if (node2.ChildNodes.Count > 0)
                {
                    this.OperNode(node2, myTreeView, 1, listStr);
                }
            }
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!ManagerLogin.ValidateRole("C_ManagerRoleEdit.aspx"))
        {
            this.Msg.CssClass = "errorMsg";
            this.Msg.Text = "您无此权限！";
            this.Msg.Visible = true;
            this.Edit.Enabled = false;
        }
        else
        {
            this.Msg.Visible = false;
            if (!base.IsPostBack)
            {
                this.Bind();
                this.ManagerTreeView.Attributes.Add("onclick", "OnTreeNodeChecked()");
            }
        }
    }
}
